#include "StdAfx.h"
#include "EnumMgr.h"
#include "Psapi.h"
#pragma comment(lib, "psapi.lib")

HWND CEnumMgr::mAryWnd[1024] = {0};
CEnumMgr::CEnumMgr(void)
{
}

CEnumMgr::~CEnumMgr(void)
{
}

DWORD CEnumMgr::EnumPID(DWORD* dw)
{
    DWORD dwSize;
    EnumProcesses(dw, 256, &dwSize); 
    return dwSize/4;
}
DWORD CEnumMgr::GetPIDByName(CString name)
{
    DWORD AryPID[512];
    TCHAR szProcessName[512];
    DWORD dwNums = EnumPID(AryPID);
    for (DWORD dw=0; dw< dwNums; ++dw)
    {
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, AryPID[dw] );
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;

            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                    sizeof(szProcessName)/sizeof(TCHAR) );
                if (!lstrcmpi(name.GetString(), szProcessName)) 
                {
                    CloseHandle(hProcess);
                    return AryPID[dw];
                }
            }
            
        }

        CloseHandle(hProcess);
    }
    return 0;
}

BOOL CALLBACK CEnumMgr::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    static int i = 0;
    mAryWnd[i] = hwnd;
    ++i;
    return 1;
}

//HWND* EnumWnd(int &num)
//{
//    ::EnumWindows(CEnumMgr::EnumWindowProc, &num);
//}

